package com.bracketbird.client.gui.club.tournament.rtc;


//import java.util.*;

import com.bracketbird.client.gui.club.tournament.*;
import com.bracketbird.client.model.*;
import com.bracketbird.client.model.tournament.*;
import com.google.gwt.user.client.Timer;
import com.bracketbird.clientcore.gui.*;
import com.bracketbird.clientcore.style.*;

import java.util.*;

/**
 *
 */
public class TeamsTable extends VerticalComponent {

    private TeamsTableRow lastFocusedRow;
    private long lastFocusTime = 0;

    private List<TeamsTableRow> rows = new ArrayList<TeamsTableRow>();

    public TeamsTable() {
        super();

        HorizontalComponent headers = new HorizontalComponent();
        headers.add(new LabelComponent("Team name"), new TextLayout("20px", "510px").paddingLeft(4).sizeNormal().bold().italic());
        headers.add(new LabelComponent("Comment"), new TextLayout("20px", "205px").sizeNormal().bold().italic());
        headers.add(new LabelComponent("Seeding"), new TextLayout("20px", "100%").sizeNormal().alignRight().bold().italic());
        add(headers, new TextLayout(5, 0, 5, 0, null, "100%").borderBottom(1).borderColor(new P(Name.COLOR, "#5C5C5C")).add(Name.COLOR, "#5C5C5C"));


        RTC.getInstance().getTournament().addTeamsListener(new TournamentListener<TournamentTeamEvent>() {
            public void onChange(TournamentTeamEvent event) {
                if (event.getAction() == TournamentTeamEvent.Action.create) {
                    teamCreated(event.getTeam(), event.isClientEvent());
                }
                else if (event.getAction() == TournamentTeamEvent.Action.delete) {
                    teamDeleted(event.getTeam(), event.isClientEvent());
                }
            }

        });
    }

    private void teamDeleted(PlayingTeam team, boolean isClientEvent) {
        int index = 0;

        for (TeamsTableRow row : rows) {
            if (row.getTeam().equals(team)) {
                rows.remove(index).removeFromParent();
                break;
            }
            index++;
        }
        final int newIndex = index;
        if (isClientEvent) {
            Timer t = new Timer() {
                @Override
                public void run() {
                    if (rows.size() > newIndex) {
                        rows.get(newIndex).setFocus(true);
                    }
                    else if (!rows.isEmpty()) {
                        rows.get(newIndex - 1).setFocus(true);
                    }
                }
            };
            t.schedule(150);
        }

    }


    public void setFocus(boolean focus) {
        if (!rows.isEmpty()) {
            rows.get(0).setFocus(true);
        }
    }

    private void teamCreated(PlayingTeam team, boolean isClient) {
        TeamsTableRow teamsTableRow = new TeamsTableRow(team, new TableNotifier() {
            public void up(TeamsTableRow tr, int coulmn) {
                int index = rows.indexOf(tr);
                if (index > 0) {
                    setFocus(rows.get(index - 1), coulmn);
                }
            }

            public void down(TeamsTableRow tr, int column) {
                int index = rows.indexOf(tr);
                if (index < rows.size() - 1) {
                    setFocus(rows.get(index + 1), column);
                }
            }
            public void focusLost(TeamsTableRow tr){
                lastFocusedRow = tr;
                lastFocusTime = System.currentTimeMillis();
            }

        });

        add(teamsTableRow, new TextLayout(null, "100%"));
        rows.add(teamsTableRow);
        if (isClient) {
            teamsTableRow.setFocus(true);
        }
    }


    private void setFocus(TeamsTableRow tr, int column) {
        if (column == 0) {
            tr.getColumnTeamName().setFocus();
        }
        else if (column == 1) {
            tr.getColumnComment().setFocus();
        }
        else {
            tr.getColumnSeeding().setFocus();
        }
    }

    public TeamsTableRow getFocusedRow(){
        for (TeamsTableRow row : rows) {
            if(row.hasFocus()){
                return row;
            }
        }
        return null;
    }

    public void deleteLastFocusedTeam(){
        if(lastFocusTime + 1000 > System.currentTimeMillis()){
            RTC.getInstance().deleteTeam(lastFocusedRow.getTeam(), true);
        }
    }
}
